/**
 * Created by liubin on 2014/12/11.
 */
define(['user/module'], function (module) {

    'use strict';

    module.registerController('UserEditCtrl', function($scope, $state, $stateParams, $log, $q, User, UserPwd, Tools
        , SmallAlertService){
        $log.log('UserEditCtrl');
        $log.log($stateParams.id);
        User.get(
            {
                id: $stateParams.id
            },
            {},
            function(success){
                $log.log(success.data);
                $scope.user = success.data;
                // for editable
                $scope.real_name = $scope.user.real_name;
            }
        );
        $scope.pwd = '';
        $scope.editableOptions =  {
    		mode: 'popup',
    		disabled: false
    	};
        /**
         * 更新画面字段
         * @returns {promise.promise|jQuery.promise|d.promise|promise|r.promise|jQuery.ready.promise|*}
         */
        $scope.update = function(){
            var q = $q.defer();
            var promise = q.promise;
                User.put(
                {
                    id:$stateParams.id
                },
                {
                    name: $scope.user.name,
                    real_name: $scope.user.real_name,
                    pwd: $scope.user.pwd
                },
                function (success) {
                    // 更新成功
                    // $log.log(success);
                    q.resolve();
                },
                function(failure){
                    q.reject(failure.messages[0]);
                }
                )
            return promise;
        }
        /**
         * 密码更新
         */
        $scope.updatePwd = function(){
            console.log(UserPwd);
            UserPwd.post(
                {
                    id:$stateParams.id
                },
                {
                    pwd: $scope.pwd
                },
                function (success) {
                    // 更新成功
                    // 刷新状态
                    SmallAlertService.info('更新成功', '密码修改成功');
                    Tools.reloadState();
                }
            );
        }
        /**
         * 返回列表画面
         */
        $scope.back = function(){
            $state.go('app.user.list');
        }
    });
});